/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controladores;

import controladores.exceptions.IllegalOrphanException;
import controladores.exceptions.NonexistentEntityException;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import modelo.Empleado;
import modelo.Nomina;
import modelo.Libranza;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import modelo.Detallenomina;

/**
 *
 * @author Ususario
 */
public class DetallenominaJpaController implements Serializable {

    public DetallenominaJpaController() {
    }

    public EntityManager getEntityManager() {
        return jpaConnection.getEntityManager();
    }

    public void create(Detallenomina detallenomina) {
        if (detallenomina.getLibranzaList() == null) {
            detallenomina.setLibranzaList(new ArrayList<Libranza>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Empleado empleadoIdempleado = detallenomina.getEmpleadoIdempleado();
            if (empleadoIdempleado != null) {
                empleadoIdempleado = em.getReference(empleadoIdempleado.getClass(), empleadoIdempleado.getIdempleado());
                detallenomina.setEmpleadoIdempleado(empleadoIdempleado);
            }
            Nomina nominaIdnomina = detallenomina.getNominaIdnomina();
            if (nominaIdnomina != null) {
                nominaIdnomina = em.getReference(nominaIdnomina.getClass(), nominaIdnomina.getIdnomina());
                detallenomina.setNominaIdnomina(nominaIdnomina);
            }
            List<Libranza> attachedLibranzaList = new ArrayList<Libranza>();
            for (Libranza libranzaListLibranzaToAttach : detallenomina.getLibranzaList()) {
                libranzaListLibranzaToAttach = em.getReference(libranzaListLibranzaToAttach.getClass(), libranzaListLibranzaToAttach.getIdlibranza());
                attachedLibranzaList.add(libranzaListLibranzaToAttach);
            }
            detallenomina.setLibranzaList(attachedLibranzaList);
            em.persist(detallenomina);
            if (empleadoIdempleado != null) {
                empleadoIdempleado.getDetallenominaList().add(detallenomina);
                empleadoIdempleado = em.merge(empleadoIdempleado);
            }
            if (nominaIdnomina != null) {
                nominaIdnomina.getDetallenominaList().add(detallenomina);
                nominaIdnomina = em.merge(nominaIdnomina);
            }
            for (Libranza libranzaListLibranza : detallenomina.getLibranzaList()) {
                Detallenomina oldDetallenominaIddetallenominaOfLibranzaListLibranza = libranzaListLibranza.getDetallenominaIddetallenomina();
                libranzaListLibranza.setDetallenominaIddetallenomina(detallenomina);
                libranzaListLibranza = em.merge(libranzaListLibranza);
                if (oldDetallenominaIddetallenominaOfLibranzaListLibranza != null) {
                    oldDetallenominaIddetallenominaOfLibranzaListLibranza.getLibranzaList().remove(libranzaListLibranza);
                    oldDetallenominaIddetallenominaOfLibranzaListLibranza = em.merge(oldDetallenominaIddetallenominaOfLibranzaListLibranza);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Detallenomina detallenomina) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Detallenomina persistentDetallenomina = em.find(Detallenomina.class, detallenomina.getIddetallenomina());
            Empleado empleadoIdempleadoOld = persistentDetallenomina.getEmpleadoIdempleado();
            Empleado empleadoIdempleadoNew = detallenomina.getEmpleadoIdempleado();
            Nomina nominaIdnominaOld = persistentDetallenomina.getNominaIdnomina();
            Nomina nominaIdnominaNew = detallenomina.getNominaIdnomina();
            List<Libranza> libranzaListOld = persistentDetallenomina.getLibranzaList();
            List<Libranza> libranzaListNew = detallenomina.getLibranzaList();
            List<String> illegalOrphanMessages = null;
            for (Libranza libranzaListOldLibranza : libranzaListOld) {
                if (!libranzaListNew.contains(libranzaListOldLibranza)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Libranza " + libranzaListOldLibranza + " since its detallenominaIddetallenomina field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            if (empleadoIdempleadoNew != null) {
                empleadoIdempleadoNew = em.getReference(empleadoIdempleadoNew.getClass(), empleadoIdempleadoNew.getIdempleado());
                detallenomina.setEmpleadoIdempleado(empleadoIdempleadoNew);
            }
            if (nominaIdnominaNew != null) {
                nominaIdnominaNew = em.getReference(nominaIdnominaNew.getClass(), nominaIdnominaNew.getIdnomina());
                detallenomina.setNominaIdnomina(nominaIdnominaNew);
            }
            List<Libranza> attachedLibranzaListNew = new ArrayList<Libranza>();
            for (Libranza libranzaListNewLibranzaToAttach : libranzaListNew) {
                libranzaListNewLibranzaToAttach = em.getReference(libranzaListNewLibranzaToAttach.getClass(), libranzaListNewLibranzaToAttach.getIdlibranza());
                attachedLibranzaListNew.add(libranzaListNewLibranzaToAttach);
            }
            libranzaListNew = attachedLibranzaListNew;
            detallenomina.setLibranzaList(libranzaListNew);
            detallenomina = em.merge(detallenomina);
            if (empleadoIdempleadoOld != null && !empleadoIdempleadoOld.equals(empleadoIdempleadoNew)) {
                empleadoIdempleadoOld.getDetallenominaList().remove(detallenomina);
                empleadoIdempleadoOld = em.merge(empleadoIdempleadoOld);
            }
            if (empleadoIdempleadoNew != null && !empleadoIdempleadoNew.equals(empleadoIdempleadoOld)) {
                empleadoIdempleadoNew.getDetallenominaList().add(detallenomina);
                empleadoIdempleadoNew = em.merge(empleadoIdempleadoNew);
            }
            if (nominaIdnominaOld != null && !nominaIdnominaOld.equals(nominaIdnominaNew)) {
                nominaIdnominaOld.getDetallenominaList().remove(detallenomina);
                nominaIdnominaOld = em.merge(nominaIdnominaOld);
            }
            if (nominaIdnominaNew != null && !nominaIdnominaNew.equals(nominaIdnominaOld)) {
                nominaIdnominaNew.getDetallenominaList().add(detallenomina);
                nominaIdnominaNew = em.merge(nominaIdnominaNew);
            }
            for (Libranza libranzaListNewLibranza : libranzaListNew) {
                if (!libranzaListOld.contains(libranzaListNewLibranza)) {
                    Detallenomina oldDetallenominaIddetallenominaOfLibranzaListNewLibranza = libranzaListNewLibranza.getDetallenominaIddetallenomina();
                    libranzaListNewLibranza.setDetallenominaIddetallenomina(detallenomina);
                    libranzaListNewLibranza = em.merge(libranzaListNewLibranza);
                    if (oldDetallenominaIddetallenominaOfLibranzaListNewLibranza != null && !oldDetallenominaIddetallenominaOfLibranzaListNewLibranza.equals(detallenomina)) {
                        oldDetallenominaIddetallenominaOfLibranzaListNewLibranza.getLibranzaList().remove(libranzaListNewLibranza);
                        oldDetallenominaIddetallenominaOfLibranzaListNewLibranza = em.merge(oldDetallenominaIddetallenominaOfLibranzaListNewLibranza);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = detallenomina.getIddetallenomina();
                if (findDetallenomina(id) == null) {
                    throw new NonexistentEntityException("The detallenomina with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Detallenomina detallenomina;
            try {
                detallenomina = em.getReference(Detallenomina.class, id);
                detallenomina.getIddetallenomina();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The detallenomina with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            List<Libranza> libranzaListOrphanCheck = detallenomina.getLibranzaList();
            for (Libranza libranzaListOrphanCheckLibranza : libranzaListOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Detallenomina (" + detallenomina + ") cannot be destroyed since the Libranza " + libranzaListOrphanCheckLibranza + " in its libranzaList field has a non-nullable detallenominaIddetallenomina field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            Empleado empleadoIdempleado = detallenomina.getEmpleadoIdempleado();
            if (empleadoIdempleado != null) {
                empleadoIdempleado.getDetallenominaList().remove(detallenomina);
                empleadoIdempleado = em.merge(empleadoIdempleado);
            }
            Nomina nominaIdnomina = detallenomina.getNominaIdnomina();
            if (nominaIdnomina != null) {
                nominaIdnomina.getDetallenominaList().remove(detallenomina);
                nominaIdnomina = em.merge(nominaIdnomina);
            }
            em.remove(detallenomina);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Detallenomina> findDetallenominaEntities() {
        return findDetallenominaEntities(true, -1, -1);
    }

    public List<Detallenomina> findDetallenominaEntities(int maxResults, int firstResult) {
        return findDetallenominaEntities(false, maxResults, firstResult);
    }

    private List<Detallenomina> findDetallenominaEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Detallenomina.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Detallenomina findDetallenomina(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Detallenomina.class, id);
        } finally {
            em.close();
        }
    }

    public int getDetallenominaCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Detallenomina> rt = cq.from(Detallenomina.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
}
